Skip to main content

Overview

Duet uses Cloudflare Durable Objects to maintain isolated, persistent state for each collaboration room. Each room gets its own DuetAgent instance that stores conversation history and coordinates with the room’s dedicated sandbox.

DuetAgent State

Each DuetAgent maintains a simple state structure:
Source: ~/workspace/source/cf-worker/index.ts:18-90

State Isolation

Each room is identified by a unique roomId. The worker extracts this from the URL and routes to the appropriate DuetAgent instance:
Source: ~/workspace/source/cf-worker/index.ts:67 This ensures:
  • Each room has isolated conversation history
  • Multiple rooms can operate simultaneously without interference
  • State persists across requests for the same room

Message History Management

The DuetAgent maintains conversation history with automatic limits:

Recent Context for AI

When generating responses, the agent uses the last 10 messages as context:
Source: ~/workspace/source/cf-worker/index.ts:154-168

Total History Limit

The agent stores up to 50 messages in total to prevent unbounded growth:
Source: ~/workspace/source/cf-worker/index.ts:179-180

State Updates

Every message interaction updates the state:
  1. User message is added to history
  2. AI generates response
  3. Agent message is added to history
  4. State is updated with both messages
Source: ~/workspace/source/cf-worker/index.ts:147-182

Room Cleanup

When a room is deleted, the DuetAgent resets its state and destroys the associated sandbox:
Source: ~/workspace/source/cf-worker/index.ts:244-266 The cleanup is triggered via DELETE request from the client:
Source: ~/workspace/source/internal/ai/client.go:106-125

Durable Objects Configuration

The worker configuration defines two Durable Object bindings:
Source: ~/workspace/source/cf-worker/wrangler.toml:6-21 Migrations define the SQLite-backed storage for each Durable Object class.

Next Steps